class Solution {
public:
    vector<string> summaryRanges(vector<int>& nums) {
        vector<string> ans;
        if (nums.size() == 0)
            return ans;

        for (int slow = 0, fast = 0; fast < nums.size(); ++fast)
        {
            if (fast == nums.size() - 1 || (long long)nums[fast + 1] - (long long)nums[fast] > 1)
            {
                int len = fast - slow + 1;
                if (len == 1)
                    ans.push_back(to_string(nums[slow]));
                else
                    ans.emplace_back(to_string(nums[slow]) + "->" + to_string(nums[fast]));
                
                slow = fast + 1;
            }
        }

        return ans;
    }
};
class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        sort(intervals.begin(), intervals.end());

        vector<vector<int>> ans;
        ans.push_back(intervals[0]);
        for (int i = 1; i < intervals.size(); ++i)
        {
            auto& inter = intervals[i];
            auto& back = ans.back();

            if (inter[0] > back[1])
                ans.push_back(inter);
            else
                back[1] = max(back[1], inter[1]);

        }

        return ans;
    }
};

class Solution {
private:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        vector<vector<int>> ans;
        ans.push_back(intervals[0]);
        for (int i = 1; i < intervals.size(); ++i)
        {
            auto& inter = intervals[i];
            auto& back = ans.back();

            if (inter[0] > back[1])
                ans.push_back(inter);
            else
                back[1] = max(back[1], inter[1]);

        }

        return ans;
    }
public:
    vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
        vector<vector<int>> ans;

        int begin1 = 0;
        int end1 = intervals.size();
        bool flag = true;

        while (begin1 < end1)
        {
            if (newInterval[0] < intervals[begin1][0] && flag)
            {
                flag = false;
                ans.push_back(newInterval);
            }
            ans.push_back(intervals[begin1++]);
        }
        if (flag)
            ans.push_back(newInterval);

        return merge(ans);
    }
};